<template>
  <van-button class="btn-item"
    :icon="value === 1 ? 'good-job' :'good-job-o'"
    :class="{liked:value === 1}"
    :isLoading="loading"
    @click="onLike" />
</template>

<script>
import { addLike, cancelLike } from '@/api/user'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: [Number, String],
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false,
      status: 0
    }
  },
  methods: {
    async onLike () {
      this.loading = true
      try {
        // let status = 0
        if (this.value === 1) {
          // 已点赞
          await cancelLike(this.articleId)
          this.status = -1
        } else {
          // 未点赞
          await addLike(this.articleId)
          this.status = 1
        }
        // 更新视图
        this.$emit('input', this.status)
      } catch (err) {
        // console.log(err)
        this.$toast.fail('操作失败，请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
